package cn.com.api.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.api.entity.*;
import cn.com.api.mapper.*;
import cn.com.api.service.*;
import cn.com.api.service.impl.base.*;
import cn.com.util.*;
import cn.com.util.*;

@Service
public class ApiT_cityServiceImpl extends T_cityServiceImplBase implements ApiT_cityService {
	@Autowired
	private ApiT_cityMapper apiT_cityMapper;
	@Autowired
	private ApiShopsMapper apiShopsMapper;
	@Autowired
	private ApiT_shops_timeMapper apiT_shops_timeMapper;
	@Autowired
	private ApiT_user_adminMapper apiT_user_adminMapper;
	@Autowired
	private ApiT_user_roleMapper apiT_user_roleMapper;

	// 新建站点和分店
	@Transactional
	public int insertT_cityAndShop(ApiT_city t_city) {
		int ret_I = apiT_cityMapper.insertT_cityAll(t_city);

		ApiShop t_shops = new ApiShop();

		t_shops.setcUname(t_city.getU_name());
		t_shops.setcPass(CipherUtil.generatePassword("123456"));
		t_shops.setcSname(t_city.getName() + "自营店");
		t_shops.setcAddress("");
		t_shops.setcAd("");
		t_shops.setcDes("");
		t_shops.setcFee("0");
		t_shops.setC_city_id(Integer.valueOf(t_city.getId()));
		t_shops.setcType(-1);
		// t_shops.setcLogo("");
		t_shops.setcMobi("");
		t_shops.setcAppdate(new Date());
		t_shops.setcOrder(0);
		t_shops.setcPsf(0);
		t_shops.setStart_fee(0);
		t_shops.setAverage_shipping_time("0");
		t_shops.setLongitude(t_city.getLongitude());
		t_shops.setLatitude(t_city.getLatitude());
		t_shops.setImage1("");
		t_shops.setBalance("");
		t_shops.setBankno("");
		t_shops.setBankname("");
		t_shops.setAcceptname("");
		t_shops.setcQq("");
		t_shops.setcMail("");
		t_shops.setDiscount("");
		t_shops.setPersonid("");
		// t_shops.setWxid(wxid);
		t_shops.setIs_open(1);
		t_shops.setIs_ziying(1);
		t_shops.setIs_lunchbox(0);// 自营店不启用餐盒费
		t_shops.setcEnable(1);// 自营店不需要审核
		t_shops.setSort(999);

		apiShopsMapper.insertT_shops(t_shops);

		System.out.println("shop_id:" + t_shops.getcId());
		// 营业时间插入到t_shops_time
		String openTime = t_city.getOpen_time();
		if (!Util.isNull(openTime)) {
			ApiT_shops_time shopTime = new ApiT_shops_time();
			shopTime.setShop_id(t_shops.getcId());
			shopTime.setBusiness_type("1,2,3,4,5,6,7");
			String[] openTimeArray = openTime.split("--");
			System.out.println("营业时间：" + openTimeArray[0] + "--" + openTimeArray[1]);
			shopTime.setStart_time(openTimeArray[0]);
			shopTime.setEnd_time(openTimeArray[1]);
			shopTime.setStatus(1);
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String date = dateFormat.format(new Date());
			shopTime.setCreate_date(date);
			shopTime.setUpdate_date(date);
			apiT_shops_timeMapper.insertT_shops_time(shopTime);
		}

		updateUserAdmin(t_city);

		return ret_I;
	}

	/**
	 * 同步更新站长管理员信息(有站长管理员就增加,没有就新增)
	 * 
	 * @param cityId
	 */
	private void updateUserAdmin(ApiT_city t_city) {
		try {
			ApiT_user_admin admin = null; // apiT_user_adminMapper.getStationRoleAdmin(t_city.getId());

			if (admin == null) {
				ApiT_user_role userRole = apiT_user_roleMapper.getStationRole();
				ApiT_user_admin newAdmin = new ApiT_user_admin();
				if (userRole == null) {
					return;
				}
				newAdmin.setRole_id(userRole.getRole_id());
				newAdmin.setTrue_name(t_city.getContacts());
				newAdmin.setCity_id(t_city.getId() + "");
				newAdmin.setReserve_money(t_city.getReserve_money());// 存储备用金
				apiT_user_adminMapper.insertT_user_admin(newAdmin);
			} else {// 更新
				admin.setTrue_name(t_city.getContacts());
				admin.setReserve_money(t_city.getReserve_money());// 更新备用金
				apiT_user_adminMapper.updateT_user_admin(admin);
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	@Override
	public void updateT_cityAndShop(ApiT_city t_city) {
		apiT_cityMapper.updateT_city(t_city);

		updateUserAdmin(t_city);
		// 查出店铺
		String openTime = t_city.getOpen_time();
		if (!Util.isNull(openTime)) {
			List<ApiShop> shops = apiShopsMapper.findZYShopByCityId(t_city.getId());
			if (shops != null && shops.size() > 0) {
				// 查出自营店铺的营业时间
				ApiShop shop = shops.get(0);
				Integer shop_id = shop.getcId();
				shop.setLongitude(t_city.getLongitude());
				shop.setLatitude(t_city.getLatitude());
				apiShopsMapper.updateT_shops(shop);

				List<ApiT_shops_time> shopTimes = apiT_shops_timeMapper.findT_shops_timeByShopId(shop_id + "");
				if (shopTimes != null && shopTimes.size() > 0) {
					ApiT_shops_time shopTime = shopTimes.get(0);
					String[] openTimeArray = openTime.split("--");
					System.out.println("营业时间：" + openTimeArray[0] + "--" + openTimeArray[1]);
					shopTime.setStart_time(openTimeArray[0]);
					shopTime.setEnd_time(openTimeArray[1]);
					SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					String date = dateFormat.format(new Date());
					shopTime.setUpdate_date(date);
					apiT_shops_timeMapper.updateT_shops_time(shopTime);
				} else {
					ApiT_shops_time shopTime = new ApiT_shops_time();
					shopTime.setShop_id(shop_id);
					shopTime.setBusiness_type("1,2,3,4,5,6,7");
					String[] openTimeArray = openTime.split("--");
					System.out.println("营业时间：" + openTimeArray[0] + "--" + openTimeArray[1]);
					shopTime.setStart_time(openTimeArray[0]);
					shopTime.setEnd_time(openTimeArray[1]);
					shopTime.setStatus(1);
					SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					String date = dateFormat.format(new Date());
					shopTime.setCreate_date(date);
					shopTime.setUpdate_date(date);
					apiT_shops_timeMapper.insertT_shops_time(shopTime);
				}
			}
		}

	}

	// 查询附近开通的县市
	public Map<String, Object> getNearbyCity(Float latitude, Float longitude) {
		Map<String, Object> city = apiT_cityMapper.getNearbyCity(latitude, longitude);
		return city;
	}

	// 查询
	public List<ApiT_city> findT_citysByIsOpen(Integer is_open) {
		List<ApiT_city> list = apiT_cityMapper.findT_citysByIsOpen(is_open);
		// System.out.println(list);
		return list;
	}

	// 查询
	public List<ApiT_city> findT_citysLikeName(String name) {
		List<ApiT_city> list = apiT_cityMapper.findT_citysLikeName(name);
		return list;
	}

	@Override
	public Map<String, Object> wxT_citysLikeName(String name) {
		return apiT_cityMapper.wxT_citysLikeName(name);
	}

}
